package com.soulgame.sms.pay.dialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.soulgame.sms.util.ResourcesUtil;

public class PayDialog extends Dialog implements OnClickListener {
	Context mContext;
	private int[] resId;
	private ImageButton btn_cancel;
	private Button btn_pay;
	public IPayDialogListener payListener;

	private View pay_layout;
	private View progress_layout;

	String title = "";
	String title_money = "";

	// 回调接口
	public interface IPayDialogListener {
		public void payConfirm(int index);

		public void payCancel();
	}

	public PayDialog(Context context) {
		super(context);
		mContext = context;
		int pay_dialog = ResourcesUtil.getResource(context, "sg_adm_sdk_fee_layout", "layout");
		setContentView(pay_dialog);
	}

	public PayDialog(Activity activity, int theme) {
		super(activity, theme);
		mContext = activity;

		initContent(activity, getWindow());

	}

	public PayDialog(Activity activity, int theme, String title, String title_money, IPayDialogListener listener) {
		super(activity, theme);
		this.title = title;
		this.title_money = title_money + "元/次";
		this.payListener = listener;

		mContext = activity;

		initContent(activity, getWindow());
	}

	private void initContent(Activity activity, Window window) {
		int pay_dialog = ResourcesUtil.getResource(activity, "sg_adm_sdk_fee_layout", "layout");

		View mContainerView = getLayoutInflater().inflate(pay_dialog, null);
		setContentView(mContainerView);

		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取屏幕宽
		int width = (int) (dm.widthPixels);// 宽度按屏幕大小的百分比设置
		int height = (int) (dm.heightPixels);

		window.setBackgroundDrawableResource(android.R.color.transparent);
		WindowManager.LayoutParams wlp = window.getAttributes();

		int dialog_w = width > height ? height : width;
		wlp.width = (int) (dialog_w * 1);

		window.setAttributes(wlp);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setCancelable(false);

		pay_layout = findViewById(ResourcesUtil.getResource(mContext, "adm_pay_layout", "id"));
		progress_layout = findViewById(ResourcesUtil.getResource(mContext, "progress_pay", "id"));

		// if(pay_layout!=null)pay_layout.setVisibility(View.INVISIBLE);
		if (progress_layout != null)
			progress_layout.setVisibility(View.INVISIBLE);

		int titleRes = ResourcesUtil.getResource(mContext, "adm_title", "id");
		TextView tv_title = (TextView) findViewById(titleRes);
		tv_title.setText(this.title);

		int title_moneyRes = ResourcesUtil.getResource(mContext, "amd_title_money", "id");
		TextView tv_title_money = (TextView) findViewById(title_moneyRes);
		tv_title_money.setText(this.title_money);

		// 取消支付
		int btn_cancelRes = ResourcesUtil.getResource(mContext, "adm_btn_cancel", "id");
		btn_cancel = (ImageButton) findViewById(btn_cancelRes);
		btn_cancel.setOnClickListener(this);

		// 确定支付
		int btn_payRes = ResourcesUtil.getResource(mContext, "amd_pay_ok", "id");
		btn_pay = (Button) findViewById(btn_payRes);
		btn_pay.setOnClickListener(this);
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		for (int i = 0; i < resId.length; i++) {
			map = new HashMap<String, Object>();
			map.put("img", resId[i]);
			list.add(map);
		}
		return list;
	}

	public void setTitle(String text) {
		int title = ResourcesUtil.getResource(mContext, "adm_title", "id");
		TextView tv_title = (TextView) findViewById(title);
		if (tv_title != null) {
			tv_title.setText(text);
		}
	}

	@Override
	public void onClick(View v) {
		int btn_cancelRes = ResourcesUtil.getResource(mContext, "adm_btn_cancel", "id");
		int btn_payRes = ResourcesUtil.getResource(mContext, "amd_pay_ok", "id");

		if (v == findViewById(btn_cancelRes)) {

			PayDialog.this.dismiss();
			payListener.payCancel();
		} else if (v == findViewById(btn_payRes)) {
			payListener.payConfirm(0);
			showProgress();
		}
	}

	private void showProgress() {
		if (pay_layout != null) {
			pay_layout.setVisibility(View.INVISIBLE);
		}

		if (progress_layout != null) {
			progress_layout.setVisibility(View.VISIBLE);
		}
	}
}
